package com.af.system.enums;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author : zhenyun.su
 * @comment : 1.定义命名规则， 2. 支持五种命名规则来实现字符串命名转换
 * @since : 2022-04-21
 */

public enum AfNamingRuleEnum {
    uhump("首字母大写驼峰命名"){
        @Override
        public String naming(String value){
            return lineToUHump(value);
        }
    },
    lhump("首字母小写驼峰命名"){
        @Override
        public String naming(String value){
            return lineToLHump(value);
        }
    },
    uline("字母大写下划线命名"){
        @Override
        public String naming(String value){
            return humpToULine(value);
        }
    },
    lline("字母小写下划线命名"){
        @Override
        public String naming(String value){
            return humpToLLine(value);
        }
    },
    upper("大写命名"){
        @Override
        public String naming(String value){
            return value.toUpperCase();
        }
    },
    lower("小写命名"){
        @Override
        public String naming(String value){
            return value.toLowerCase();
        }
    },
    same("命名不变保持一样"){
        @Override
        public String naming(String value){
            return value;
        }
    };

    private String desc;
    private static Pattern linePattern = Pattern.compile("_(\\w)");
    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    public abstract String naming(String value);

    AfNamingRuleEnum(String desc) {
        this.desc = desc;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    /*
     * @comment : 根据字符串获取枚举, 未能获取返回null
     */
    public static AfNamingRuleEnum getRule(String rule) throws Exception {
        if (rule == null || rule ==""){
            return AfNamingRuleEnum.same;
        }
        try {
            return AfNamingRuleEnum.valueOf(rule.toLowerCase());
        } catch (Exception e) {
            throw new Exception(rule+" is not a preset value");
        }
    }

    /*
     * @comment :  按命名规则进行转换
     */
    public static String renameForRules(String value, AfNamingRuleEnum rules){
        switch (rules) {
            case lhump: return lineToLHump(value);
            case uhump: return lineToUHump(value);
            case lline: return humpToLLine(value);
            case uline: return humpToULine(value);
            case upper: return value.toUpperCase();
            case lower: return value.toLowerCase();
            case same: return value;
            default: return value;
        }
    }

    /**
     * @comment :  下划线转首字母小写驼峰 sys_user = sysUser
     */
    public static String lineToLHump(String str) {
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);

        return sb.toString();
    }
    /**
     * @comment :  下划线转首字母大写驼峰 sys_user = SysUser
     */
    public static String lineToUHump(String str) {
        String tmp = lineToLHump(str);
        return tmp.substring(0, 1).toUpperCase().concat(tmp.substring(1));
    }

    /**
     * @comment : 首字母小写驼峰转首字母大写驼峰 sysUser = SysUser
     */
    public static String lHumpToUHump(String str) {
        return str.substring(0, 1).toUpperCase().concat(str.substring(1));
    }

    /**
     * @comment :  驼峰转小写下划线  sysUser = sys_user
     */
    public static String humpToLLine(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * @comment :  驼峰转大写下划线  sysUser = SYS_USER
     */
    public static String humpToULine(String str) {
        return humpToLLine(str).toUpperCase();
    }

}
